package com.thinkgem.jeesite.frame.pm.web;

import com.thinkgem.jeesite.common.persistence.BaseConstants;
import com.thinkgem.jeesite.common.persistence.BasePage;
import com.thinkgem.jeesite.common.service.ServiceException;
import com.thinkgem.jeesite.common.utils.StringUtils;
import com.thinkgem.jeesite.common.web.BaseController;
import com.thinkgem.jeesite.frame.pm.entity.PmDataDict;
import com.thinkgem.jeesite.frame.pm.entity.PmDataDictItem;
import com.thinkgem.jeesite.frame.pm.service.PmDataDictItemService;
import com.thinkgem.jeesite.frame.pm.service.PmDataDictService;
import com.thinkgem.jeesite.frame.pm.utils.PmCacheUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 数据字典条目Controller
 *
 * @author kongxiangsheng
 * @version 2018-08-31
 */
@Controller
@RequestMapping(value = "${adminPath}/pm/pmDataDictItem")
public class PmDataDictItemController extends BaseController {
    @Autowired
    private PmDataDictItemService pmDataDictItemService;
    @Autowired
    private PmDataDictService pmDataDictService;

    @RequiresPermissions("pm:pmDataDictItem:view")
    @RequestMapping(value = {"list", ""})
    public String list(PmDataDictItem pmDataDictItem, HttpServletRequest request, HttpServletResponse response, Model model) {
        if (StringUtils.isBlank(pmDataDictItem.getDictCode())) {
            model.addAttribute("message", "查询失败:没有指定字典编码！");
            return "frame/pm/PmDataDictItem/list";
        }
        PmDataDict pmDataDict = pmDataDictService.getByDictCode(pmDataDictItem.getDictCode());
        if (null == pmDataDict || StringUtils.isBlank(pmDataDict.getDictName())) {
            model.addAttribute("message", "查询失败:没有查到字典信息！");
            return "frame/pm/PmDataDictItem/list";
        }

        BasePage<PmDataDictItem> page = new BasePage<PmDataDictItem>(request, response);
        pmDataDictItemService.selectExtPage(page, pmDataDictItem.getDictCode());
        model.addAttribute("pmDataDict", pmDataDict);
        model.addAttribute("page", page);
        return "frame/pm/PmDataDictItem/list";
    }

    @RequiresPermissions("pm:pmDataDictItem:view")
    @RequestMapping(value = "form")
    public String form(PmDataDictItem pmDataDictItem, Model model) {
        // 检查dictCode必传参数
        if (StringUtils.isBlank(pmDataDictItem.getDictCode())) {
            model.addAttribute("message", "查询失败:没有指定字典编码！");
            return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage&dictCode=" + pmDataDictItem.getDictCode();
        }
        // 查询数据字典信息
        PmDataDict pmDataDict = pmDataDictService.getByDictCode(pmDataDictItem.getDictCode());
        if (null == pmDataDict || StringUtils.isBlank(pmDataDict.getDictName())) {
            model.addAttribute("message", "查询失败:没有查到字典信息！");
            return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage&dictCode=" + pmDataDictItem.getDictCode();
        }
        if (StringUtils.isEmpty(pmDataDictItem.getId())) {
            int maxItem = pmDataDict.getMaxItems();
            List<PmDataDictItem> itemList = pmDataDictItemService.getByDictCode(pmDataDictItem.getDictCode());
            if (null != itemList && itemList.size() >= maxItem) {
//                model.addAttribute("message", "操作失败：该参数最多支持添加" + maxItem + "个条目！");
//                return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage&dictCode=" + pmDataDictItem.getDictCode();
                throw new ServiceException("操作失败：该参数最多支持添加" + maxItem + "个条目！");
            }
        }
        model.addAttribute("pmDataDict", pmDataDict);
        model.addAttribute("pmDataDictItem", pmDataDictItem);
        return "frame/pm/PmDataDictItem/form";
    }

    @RequiresPermissions("pm:pmDataDictItem:edit")
    @RequestMapping(value = "save")
    public String save(PmDataDictItem pmDataDictItem, Model model, RedirectAttributes redirectAttributes) {
        if (!beanValidator(model, pmDataDictItem)) {
            return form(pmDataDictItem, model);
        }

        // 查询数据字典信息
        PmDataDict pmDataDict = pmDataDictService.getById(pmDataDictItem.getDataDictId());
        if (null == pmDataDict || StringUtils.isBlank(pmDataDict.getDictName())) {
            addMessage(redirectAttributes, "保存失败，没有查到字典信息！");
            return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage";
        }

        // 清除缓存
        PmCacheUtils.removeDataDictItems(pmDataDict.getDictCode());

        // 执行保存操作
        pmDataDictItemService.saveOrUpdate(pmDataDictItem);
        addMessage(redirectAttributes, "保存数据字典条目成功");
        return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage&dictCode=" + pmDataDict.getDictCode();
    }

    @RequiresPermissions("pm:pmDataDictItem:edit")
    @RequestMapping(value = "delete")
    public String delete(PmDataDictItem pmDataDictItem, RedirectAttributes redirectAttributes) {
        // 查询数据字典信息
        PmDataDict pmDataDict = pmDataDictService.getById(pmDataDictItem.getDataDictId());
        if (null == pmDataDict || StringUtils.isBlank(pmDataDict.getDictName())) {
            return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage";
        }
        // 判断盖子点是否允许被删除
        if (BaseConstants.YES_NO_FLAG_N.equals(pmDataDict.getAllowDelete())) {
            addMessage(redirectAttributes, "删除失败，" + pmDataDict.getDictName() + "不允许删除操作！");
            return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage&dictCode=" + pmDataDict.getDictCode();
        }

        // 清除缓存
        PmCacheUtils.removeDataDictItems(pmDataDict.getDictCode());

        // 执行删除操作
        pmDataDictItemService.removeById(pmDataDictItem.getId());
        addMessage(redirectAttributes, "删除数据字典条目成功");
        return "redirect:" + adminPath + "/pm/pmDataDictItem/?repage&dictCode=" + pmDataDict.getDictCode();
    }

}